// Code file original author: Martin Lapierre, http://devinstinct.com

using System;
using System.Reflection;

namespace Microsoft.Practices.EnterpriseLibrary.Validation.Validators
{
	/// <summary>
	/// Describes a <see cref="TypeValidator{T}"/>.
	/// </summary>
	[AttributeUsage(AttributeTargets.Property
		 | AttributeTargets.Field
		 | AttributeTargets.Method
		 | AttributeTargets.Parameter,
		 AllowMultiple = true,
		 Inherited = false)]
	public class TypeValidatorAttribute : ValueValidatorAttribute
	{
		#region Fields

		/// <summary>
		/// The target type.
		/// </summary>
		Type targetType;

		#endregion Fields


		#region Constructors

		/// <summary>
		/// <para>Initializes a new instance of the <see cref="TypeValidatorAttribute"/> </para>
		/// </summary>
		/// <param name="targetType">The target type.</param>
		public TypeValidatorAttribute(Type targetType)
		{
			this.targetType = targetType;
		}

		#endregion Constructors


		#region Methods

		/// <summary>
		/// Creates the <see cref="Validator"/> described by the attribute object providing validator specific
		/// information.
		/// </summary>
		/// <param name="notused">Not used.</param>
		/// <returns>The created <see cref="Validator"/>.</returns>
		protected override Validator DoCreateValidator(Type notused)
		{
			Type typeValidator = typeof(TypeValidator<>).MakeGenericType(targetType);
			return Activator.CreateInstance(typeValidator, GetMessageTemplate(), Ruleset, Negated) as Validator;
		}

		#endregion Methods
	}
}
